I am trying to get an axios getPosts to work. I am getting a syntax error but I can't find what's wrong with my code.
getPosts = async () => {
let data = await api.get('/').then(({ data })
=> data);
this.setState({ posts: data })
}
The error is posted below:
./src/App.js
SyntaxError: /home/roxx/Documents/eternal_kings_web_app/Twitter-Clone/src/App.js: Unexpected token, expected "," (25:4)
23 | getPosts = async () => {
24 | let data = await api.get('/').then(({ data })
> 25 | => data);
| ^
26 | this.setState({ posts: data })
27 | }
The source of your syntax error is the newline in your arrow function.
Per MDN on Arrow Functions: "Multiline statements require body braces and return." To stick with the one-line:
let data = await api.get("/").then(({ data }) => data);
And to include the desired setState:
let data = await api.get("/").then(({ data }) => this.setState({ posts: data }));